<template>
  <van-icon
    :color="value === 1 ? 'red' : '#777'"
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    @click="onClick"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  name: 'LikeArticle',
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  components: {},
  data () {
    return {
      loading: false
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onClick () {
      if (this.loading) return
      this.loading = true
      if (this.value === 1) {
        // 取消
        await deleteLike(this.articleId)
      } else {
        // 点赞
        await addLike(this.articleId)
      }
      this.$emit('input', this.value === 1 ? 0 : 1)
      this.$toast(this.value !== 1 ? '点赞成功' : '取消点赞')
      this.loading = false
    }
  },
  watch: {},
  computed: {}
}
</script>

<style lang="less" scoped>
</style>
